package com.wntms.gps.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.wntms.gps.dao.*;
import com.wntms.gps.pojo.*;
import com.wntms.gps.service.ICarriageContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 运输合同Service业务层处理
 *
 * @author group 4
 * @date 2022-11-22
 */
@Service
public class CarriageContractServiceImpl implements ICarriageContractService
{
    @Autowired
    private CarriageContractDao carriageContractDao;

    @Autowired
    private CustomerInDao customerInDao;

    @Autowired
    private CustomerInformationDao customerInformationDao;

    @Autowired
    private DriverInformationDao driverInformationDao;

    @Autowired
    private FreightStatusDaog freightStatusDaog;

    @Autowired
    private FreightInformationMapper freightInformationMapper;

    @Autowired
    private DriverSettleMapper driverSettleMapper;

    /**
     * 根据货运单生成运输单
     * */
    @Override
    @Transactional
    public int generateCarriageContract(FreightContract freightContract) {
        CarriageContract carriageContract = new CarriageContract();

        //有六个值是直接取货运合同的
        String fid = freightContract.getFid();
        Long uid = freightContract.getUid();
        Long coid = freightContract.getCoid();
        Double insurancefare = freightContract.getInsurancefare();
        Double fare = freightContract.getFare();
        Integer statusId = Integer.parseInt(freightContract.getFsid().toString());

        carriageContract.setFid(fid);
        carriageContract.setSenderid(uid);
        carriageContract.setReceiverid(coid);
        carriageContract.setInsurance(insurancefare);
        carriageContract.setFare(fare);
        carriageContract.setStatus(statusId);
        //单号自己生成
        String uuid= UUID.randomUUID().toString().replaceAll("-","");
        //存到结算表中
        DriverSettle driverSettle = new DriverSettle();
        driverSettle.setCid(uuid);
        driverSettleMapper.insert(driverSettle);
        carriageContract.setCid(uuid);
        //发货时间就是当前
        carriageContract.setShippingTime(new Date());
        //设置预计到货时间
        carriageContract.setArrivalTime(freightContract.getArrivetime());

        //司机的自动指派
        Long fiid = freightContract.getFiid();
        String type = null;
        FreightInformation freightInformation = freightInformationMapper.selectFreightInformationById(fiid);
        Long volume = freightInformation.getVolume();
        Long weight = freightInformation.getWeight();
        if(volume <=40){
            if(weight<=6000){
                type = "small";
            }else if(weight>6000&&weight<=14000){
                type = "medium";
            }else if(weight>14000&&weight<=100000){
                type = "big";
            }else if(weight>10000){
                type = "plus";
            }else {
                type = "small";
            }
        }else if(volume >40&&volume<=80){
            if(weight<=6000){
                type = "small";
            }else if(weight>6000&&weight<=14000){
                type = "medium";
            }else if(weight>14000&&weight<=100000){
                type = "big";
            }else if(weight>10000){
                type = "plus";
            }else {
                type = "small";
            }
        }else if(volume>80){
            if(weight<=6000){
                type = "small";
            }else if(weight>6000&&weight<=14000){
                type = "medium";
            }else if(weight>14000&&weight<=100000){
                type = "big";
            }else if(weight>10000){
                type = "plus";
            }else {
                type = "small";
            }
        }else {
            type = "small";
        }

        //查询合适的司机，取首个
        DriverInformation driverInformation = new DriverInformation();
        driverInformation.setType(type);
        driverInformation.setStatus(0);
        List<DriverInformation> list = driverInformationDao.selectDriverInformationList(driverInformation);
        if(list.size()==0){
            throw new MyException("没有司机可用");
        }
        Long driverId = list.get(0).getId();
        //设置司机id
        carriageContract.setDiid(driverId);
        //设置司机目的地与状态
        DriverInformation target = new DriverInformation();
        target.setId(driverId);
        target.setStatus(1);
        target.setTargetCity(customerInDao.selectCustomerInById(freightContract.getCoid()).getAddress());
        driverInformationDao.updateDriverInformation(target);

        //新增
        return carriageContractDao.insertCarriageContract(carriageContract);
    }

    /**
     * 查询运输合同
     *
     * @param cid 运输合同主键
     * @return 运输合同
     */
    @Override
    public CarriageContract selectCarriageContractByCid(String cid)
    {
        return carriageContractDao.selectCarriageContractByCid(cid);
    }

    /**
     * 查询运输合同列表
     *
     * @param carriageContract 运输合同
     * @return 运输合同
     */
    @Override
    public List<CarriageContract> selectCarriageContractList(CarriageContract carriageContract)
    {
        return carriageContractDao.selectCarriageContractList(carriageContract);
    }

    public List<CarriageContract> findAll(){
        return carriageContractDao.findAll();
    }

    /**
     * 新增运输合同
     *
     * @param carriageContract 运输合同
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCarriageContract(CarriageContract carriageContract)
    {
        customerInDao.insertCustomerIn(carriageContract.getCustomerIn());
        customerInformationDao.insertCustomerInformation(carriageContract.getCustomerInformation());

        FreightStatus freightStatus = carriageContract.getFreightStatus();
        freightStatus.setStatus(1);
        freightStatus.setIscollection(false);
        freightStatus.setReceiveAddress(carriageContract.getCustomerIn().getAddress());
        freightStatusDaog.insertFreightStatus(freightStatus);

        String id = UUID.randomUUID().toString().replace("-","");
        carriageContract.setReceiverid(carriageContract.getCustomerIn().getId());
        carriageContract.setSenderid(carriageContract.getCustomerInformation().getId());
        carriageContract.setCid(id);
        carriageContract.setStatus(freightStatus.getId());

        //改变司机的目的地
        Long diid = carriageContract.getDiid();
        DriverInformation driverInformation = new DriverInformation();
        driverInformation.setId(diid);
        driverInformation.setTargetCity(carriageContract.getCustomerIn().getAddress());
        driverInformationDao.updateDriverInformation(driverInformation);

        return carriageContractDao.insertCarriageContract(carriageContract)
                ;
    }

    /**
     * 修改运输合同
     *
     * @param carriageContract 运输合同
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCarriageContract(CarriageContract carriageContract)
    {
        DriverInformation driverInformation = new DriverInformation();
        driverInformation.setId(carriageContract.getDiid());
        driverInformation.setTargetCity(carriageContract.getCustomerIn().getAddress());

        //将新司机置为忙
        driverInformation.setStatus(1);

        //将原司机置为空闲
        Long id = carriageContract.getDriverInformation().getId();
        DriverInformation origin = new DriverInformation();
        origin.setId(id);
        origin.setStatus(0);
        origin.setTargetCity("");
        driverInformationDao.updateDriverInformation(origin);

        return carriageContractDao.updateCarriageContract(carriageContract)
                +customerInDao.updateCustomerIn(carriageContract.getCustomerIn())
                +customerInformationDao.updateCustomerInformation(carriageContract.getCustomerInformation())
                +driverInformationDao.updateDriverInformation(driverInformation);
    }

    /**
     * 批量删除运输合同
     *
     * @param cids 需要删除的运输合同主键
     * @return 结果
     */
    @Override
    public int deleteCarriageContractByCids(String[] cids)
    {
        return carriageContractDao.deleteCarriageContractByCids(cids);
    }

    /**
     * 删除运输合同信息
     *
     * @param cid 运输合同主键
     * @return 结果
     */
    @Override
    public int deleteCarriageContractByCid(String cid)
    {
        return carriageContractDao.deleteCarriageContractByCid(cid);
    }
}